package com.bookmanager.www.controller;


import com.bookmanager.www.common.DTO.FlAddressDTO;
import com.bookmanager.www.common.utils.Result;
import com.bookmanager.www.entity.FlAddress;
import com.bookmanager.www.mapper.FlAddressMapper;
import com.bookmanager.www.mapper.FlUserMapper;
import com.bookmanager.www.service.FlAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户的地址表（用户与用户地址表对应关系应该是一对多) 前端控制器
 * </p>
 *
 * @author 风离
 * @since 2021-09-17
 */
@RestController
@RequestMapping("/fl-address")
@Api(tags="地址管理")
public class FlAddressController {

    @Resource
    FlAddressService flAddressService;

    @Resource
    FlAddressMapper flAddressMapper;

    @Resource
    FlUserMapper flUserMapper;

    @PutMapping("/modifyAddressByAddressId")
    @ApiOperation(value="修改用户地址",notes="传入用户地址唯一id 和修改后的信息即可")
    Result modifyAddressByAddressId(
                                    @RequestBody @Validated FlAddressDTO flAddressDTO)
    {
        //如果这个是默认地址那么把其他的都设置为非默认
        if(flAddressDTO.getAddressDefault()==1)
        {
           int update = flAddressMapper.updateOtherAddressDefault(flAddressDTO.getAddressId(),flAddressDTO.getUserId());
            System.out.println("修改了几个默认地址？？"+update);
        }
        int updated =flAddressMapper.modifyAddressByAddressId(flAddressDTO.getAddressId(),flAddressDTO.getName(),
                flAddressDTO.getPhoneNum(),flAddressDTO.getAddressDefault(),
                flAddressDTO.getReceiveProvince(),flAddressDTO.getReceiveCity(),
                flAddressDTO.getReceiveCounty(),flAddressDTO.getReceiveDesc()
                );
        if(updated!=0)
        {
            return Result.succ("修改用户地址成功！","");
        }

        return Result.fail("修改用户地址失败！","");
    }

    @GetMapping("/getAddressByAddressId")
    @ApiOperation(value="通过地址唯一Id查询用户地址",notes="传入地址唯一Id即可")
    Result getAddressByAddressId(@RequestParam("addressId")String addressId)
    {
        FlAddress flAddress = flAddressMapper.selectAddressByAddressId(addressId);
        if(flAddress!=null)
        {
            return Result.succ("查询成功！！",flAddress);
        }

        return Result.fail("查询失败！","");
    }

    @GetMapping("/getAddressByUserId")
    @ApiOperation(value="通过用户Id查询用户地址",notes="传入用户id即可")
    Result getAddress(@RequestParam("userId")String userId)
    {
       List<FlAddress> flAddressList = flAddressMapper.getAddressList(userId);

        return Result.succ("查询用户地址成功！",flAddressList);
    }
    @GetMapping("/getAddressByOrderId")
    @ApiOperation(value="根据订单编号查地址",notes="传入订单编号即可")
    Result getAddressByOrderId(@RequestParam("orderId")String orderId)
    {
        if(orderId.length()!=0)
        {
            FlAddress flAddress = flAddressMapper.getAddressByOrderId(orderId);
            return Result.succ("根据订单编号查询用户地址成功！",flAddress);
        }
      return Result.fail("查询用户地址失败！","");
    }

    @PostMapping("/addAddress")
    @ApiOperation(value="添加一个收货地址",notes="传入用户Id 用户所在的详细地址 手机号等~")
    Result addAddress(@RequestBody @Validated FlAddressDTO flAddressDTO)
    {
          String userId = flAddressDTO.getUserId();
          System.out.println("当前用户id为:"+userId);

          ArrayList insert =  flAddressService.addAddress(userId,flAddressDTO.getName(),
                  flAddressDTO.getPhoneNum(),flAddressDTO.getAddressDefault(),
                  flAddressDTO.getReceiveProvince(),flAddressDTO.getReceiveCity(),
                  flAddressDTO.getReceiveCounty(),flAddressDTO.getReceiveDesc()
                  );

          if (!insert.get(0).equals(0))
          {
              return Result.succ("添加地址成功！！返回当前地址唯一Id",insert.get(1));
          }

        return Result.fail("添加地址失败","");
    }

    @DeleteMapping("/deleteAddressByAddressIds")
    @ApiOperation(value="删除收货地址 可多选",notes="传入AddressId的集合即可")
    Result deleteAddressByAddressIds(@RequestParam("addressIds")String[] addressIds)
    {
        int deletes = flAddressMapper.deleteAddressByAddressIds(addressIds);
        if(deletes==addressIds.length)
        {
            return Result.succ("批量删除成功！！","");
        }
        return Result.fail("删除失败！！","");

    }

}
